%%%-----------------------------------------------------------------------------
%%% @doc
%%% web phone ets
%%%
%%% @end
%%%-----------------------------------------------------------------------------
-module(url_pid_manager).

-behavior(gen_server).

-export([start_link/0]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-record(state, {last, tref}).

-define(TIMER, 60000).
%%------------------------------------------------------------------------------
%% @doc
%% Start web phone  ets.
%%
%% @end
%%------------------------------------------------------------------------------
start_link() ->
  gen_server2:start_link({local, ?MODULE}, ?MODULE, [], []).

%%%=============================================================================
%%% gen_server callbacks
%%%=============================================================================

init([]) ->
  process_flag(trap_exit, true),
  erlang:send_after(?TIMER, self(), doit),
  {ok, #state{last = stamp()}}.

%% @spec handle_call(Args, From, State) -> tuple()
%% @doc gen_server callback.
handle_call(stop, _From, State) ->
    {stop, shutdown, stopped, State};
handle_call(_Req, _From, State) ->
    {reply, {error, badrequest}, State}.

%% @spec handle_cast(Cast, State) -> tuple()
%% @doc gen_server callback.
handle_cast(_Req, State) ->
    {noreply, State}.

%% @spec handle_info(Info, State) -> tuple()
%% @doc gen_server callback.
handle_info(doit, State) ->
    Now = stamp(),
    tools:upload_url_create_pid(),
    erlang:send_after(?TIMER, self(), doit),
    {noreply, State#state{last = Now}};
handle_info(_Info, State) ->
    {noreply, State}.

%% @spec terminate(Reason, State) -> ok
%% @doc gen_server termination callback.
terminate(_Reason, _State) ->
    % {ok, cancel} = timer:cancel(State#state.tref),
    ok.

code_change(_OldVsn, State, _Extra) ->
  {ok, State}.


stamp() ->
    erlang:localtime().